# Create your views here.
from django.shortcuts import *
from cs.models import VmTemplate
from cs.models import *
from com.instance_option import *
from com.zone_option import *
from com.templates_option import *
from com.compute_offerings import *
from com.Storage_option import *
from com.network_option import *

def index(req): 
    # vm_template_list=VmTemplate.objects.all()
    # data_center_list=DataCenter.objects.all()
    instances_list=get_all_instances_list()
    zone_list=get_all_zone_list()
    templates_list=get_all_templates_list()
    compute_offerings_list=listcompute_offerings()
    storage_list=get_all_storage_list()
    network_list=get_all_network_list()
    #return render_to_response('main.html',{'networks_list':networks_info,'freeip':free_list,'sum_ip':sum_free_ip})  
    return render_to_response('main.html',{'instances':instances_list,
                                           'zones':zone_list,
                                           'templates':templates_list,
                                           'compute_offerings':compute_offerings_list,
                                           'storages':storage_list,
                                           'networks':network_list})  
def instance_list(req): 
    # vm_template_list=VmTemplate.objects.all()
    # data_center_list=DataCenter.objects.all()
    instances_list=get_all_instances_list()
    #return render_to_response('main.html',{'networks_list':networks_info,'freeip':free_list,'sum_ip':sum_free_ip})  
    return render_to_response('instance_list.html',{'instances':instances_list}) 


def test(req): 
    # vm_template_list=VmTemplate.objects.all()
    # data_center_list=DataCenter.objects.all()
    ss=[{u'domain': u'ROOT', u'domainid': u'56d10104-fe94-4a07-857e-d1088d24f03d', u'haenable': False, u'templatename': u'TEST-CSEN', u'securitygroup': [], u'zoneid': u'e54d5a00-7ca0-4656-98bf-ef00816b0e12', u'cpunumber': 1, u'passwordenabled': False, u'instancename': u'i-2-62-VM', u'id': u'cf47ddd3-64d4-4468-934f-8d07d0c4826a', u'state': u'Stopped', u'guestosid': u'98b43aff-17cd-4be6-a4d6-63fad6441123', u'memory': 512, u'serviceofferingid': u'4af7e56f-b799-4d8e-8113-687284c1d8cb', u'zonename': u'CS01', u'displayname': u'eager-wedd', u'tags': [], u'nic': [{u'networkid': u'b8bbd986-043d-41ff-99d4-ec424eaf1fbe', u'macaddress': u'06:f2:2e:00:02:70', u'isolationuri': u'vlan://128', u'type': u'Shared', u'gateway': u'192.168.128.1', u'traffictype': u'Guest', u'broadcasturi': u'vlan://128', u'netmask': u'255.255.255.0', u'ipaddress': u'192.168.128.33', u'id': u'4edd28b0-2b9a-4a62-bd1d-b9427cc5e525', u'isdefault': True}], u'cpuspeed': 1000, u'templateid': u'9d241e77-921d-4510-bb49-52cd1e46c30a', u'account': u'admin', u'hostid': u'0', u'name': u'eager-wedd', u'created': u'2013-06-17T15:31:50+0800', u'hypervisor': u'XenServer', u'rootdevicetype': u'Not created', u'rootdeviceid': 0, u'serviceofferingname': u'share-node01', u'templatedisplaytext': u'TEST-CSEN'}]
    return render_to_response('/instance_list')  

#'ipinfo':ip_info,'temp_list':temp_list,'zone_list':zone_list,'networks_list':networks_info,

def instance_ops(request):
    free_list=get_free_IP_Parameter()
    networkname_list=get_all_network_name()
    templates_list=get_all_templates_list()
    zone_list=get_all_zone_list()
    compute_offerings_list=listcompute_offerings()
    return render_to_response('instance.html',{'networks_list':networkname_list,
                                               'freeip':free_list,
                                               'templates':templates_list,
                                               'compute_offerings':compute_offerings_list,
                                               'zones':zone_list})


def depoly_instance(request):
    commands=deploy_instance()
    return render_to_response('depoly_instance.html',{'command': commands})

def vms_status(request):
    instances_list=get_all_runing_instances_list()
    return render_to_response('vm_status.html',{'instances':instances_list})

    
def search_form(request):
    if 'ips' in request.GET and request.GET['ips']:
            vm_ip=request.GET['ips']
    else:
            return render_to_response('index.html', {'error': True})
    if 'zone_id' in request.GET and request.GET['zone_id']:
            vm_zone=request.GET['zone_id']
    else:
            return render_to_response('index.html', {'error': True})
    if 'template_id' in request.GET and request.GET['template_id']:
            vm_template=request.GET['template_id']
    else:
            return render_to_response('index.html', {'error': True})
    if 'compute_id' in request.GET and request.GET['compute_id']:
            vm_compute=request.GET['compute_id']
    else:
            return render_to_response('index.html', {'error': True})
    if 'instance_name' in request.GET and request.GET['instance_name']:
            vm_name=request.GET['instance_name']
    else:
            return render_to_response('index.html', {'error': True})
        
    if 'networkid' in request.GET and request.GET['networkid']:
            vm_networkid=request.GET['networkid']
    else:
            return render_to_response('index.html', {'error': True})
    
    
    cmd={'networkids':vm_networkid,
         'serviceofferingid':vm_compute,
         'zoneid':vm_zone,
         'templateid':vm_template,
         'name':vm_name,
         'ipaddress':vm_ip
         }
    
    instances=deploy_instance(cmd)
    print instances
    return redirect('/instance_list/')
